package nige.portable.commun;

import java.util.Random;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class MyCanvas extends Canvas implements CommandListener {
	public static String AFF = "";
	public static String AFF2 = "";
	int x=0;
	int y=0;
	int xDec=0;
	int yDec=0;
	int colR = 0;
	int colV = 0;
	int colB = 0;
	
	Graphics buffer = null;
	Image image = null;
	
	public void Canvas() {
		setCommandListener(this);
	}
	public void paint(Graphics g) {
		if(buffer==null){
			image = Image.createImage(getWidth(),getHeight());
			buffer = image.getGraphics();
		}
		buffer.setColor(Utils.getColor(255,255,255));
		buffer.fillRect(0,0,getWidth(),getHeight());
		buffer.setColor(Utils.getColor(colR,colV,colB));
		x = x + xDec;
		y = y + yDec;
		if(x>getWidth()) {
			x = 0;
		}
		if(x<0) {
			x = getWidth();
		}
		if(y>getHeight()) {
			y = 0;
		}
		if(y<0) {
			y = getHeight();
		}
		buffer.fillRect(x,y,20,20);
		Font f3 = Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_PLAIN,
				Font.SIZE_SMALL);
		buffer.setFont(f3);
		buffer.setColor(0);
		buffer.drawString(Utils.getTime(),getWidth()/3,
				getHeight()/3,0);
		
		g.drawImage(image,0,0,0);
		
		/*
		MyMidlet.buffer.setFont(f3);
		MyMidlet.buffer.setColor(0);
		MyMidlet.buffer.drawString(ColorApp.getTime()+" Aff : "+AFF,getWidth()/3,
				getHeight()/3,0);
		MyMidlet.buffer.drawString("Memory : "+AFF2,getWidth()/3,
				getHeight()/3+50,0);
		*/
	}
	public void commandAction(Command arg0, Displayable arg1) {
	}
	public void keyPressed(int code) {
		Random ran = new Random();
		if(code==50 || code==-1) {
			yDec--;
		}
		if(code==54 || code==-4) {
			xDec++;
		}
		if(code==56 || code==-2) {
			yDec++;
		}
		if(code==52 || code==-3) {
			xDec--;
		}
		if(code==53 || code==-5) {
			colR = ran.nextInt(255);
			colV = ran.nextInt(255);
			colB = ran.nextInt(255);
		}
	}
	public void keyReleased(int code) {
		if(code==50 || code==-1) {
			yDec++;
		}
		if(code==54 || code==-4) {
			xDec--;
		}
		if(code==56 || code==-2) {
			yDec--;
		}
		if(code==52 || code==-3) {
			xDec++;
		}
	}
}
